﻿Public Class FormThucDon

    Private Sub FormThucDon_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim tdBUS As New BUS.ThucDonBUS
        gcThucDon.DataSource = tdBUS.LoadThucDon()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Try
            Dim dt As DataTable
            dt = gcThucDon.DataSource
            For Each row As DataRow In dt.Rows
                If (row.RowState = DataRowState.Modified) Then
                    Dim MaMenu As Integer
                    Dim DonGia As Double
                    Dim Ten As String
                    Dim TinhTrang As Integer
                    Integer.TryParse(row(0).ToString(), MaMenu)
                    Double.TryParse(row(1).ToString(), DonGia)
                    Ten = row(2).ToString()
                    Integer.TryParse(row(3).ToString(), TinhTrang)
                    Dim _td As New DTO.ThucDonDTO(MaMenu, DonGia, Ten, TinhTrang)
                    Dim _tdBus As New BUS.ThucDonBUS
                    _tdBus.UpdateTD(_td)
                    MessageBox.Show("Thành công!", "Thông báo")
                End If
                If (row.RowState = DataRowState.Added) Then

                    Dim DonGia As Double
                    Dim Ten As String
                    Dim TinhTrang As Integer
                    Double.TryParse(row(1).ToString(), DonGia)
                    Ten = row(2).ToString()
                    Integer.TryParse(row(3).ToString(), TinhTrang)
                    Dim _td As New DTO.ThucDonDTO(0, DonGia, Ten, TinhTrang)
                    Dim _tdBus As New BUS.ThucDonBUS
                    _tdBus.AddTD(_td)

                    MessageBox.Show("Thành công!", "Thông báo")
                End If
            Next
        Catch ex As Exception
            MessageBox.Show("Không thực được thao tác !", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error)
        End Try
    End Sub

    Private Sub gcThucDon_ProcessGridKey(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles gcThucDon.ProcessGridKey
        Dim dt As DataTable
        dt = gcThucDon.DataSource
        If e.KeyCode = Keys.Delete Then
            If GridView1.FocusedRowHandle < 0 Then
                MessageBox.Show("Không thể xóa dòng này!!!???", "Lỗi !", MessageBoxButtons.OK, MessageBoxIcon.Error)
            Else
                If MessageBox.Show("Bạn có chắc chắn muốn xóa???", "Chú ý !", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
                    Dim dr As DataRow
                    dr = dt.Rows(GridView1.FocusedRowHandle)
                    Dim MaMenu As Integer
                    Integer.TryParse(dr(0).ToString(), MaMenu)
                    Dim td As New DTO.ThucDonDTO(MaMenu, 0, "", 0)
                    Dim tdBus As New BUS.ThucDonBUS
                    tdBus.DelTD(td)

                    'dt.Rows.Remove(dr)
                    dt.AcceptChanges()
                End If
            End If
        End If

    End Sub
End Class